package com.ruoyi.lwpq.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.lwpq.domain.LwpqCbry;
import com.ruoyi.lwpq.mapper.LwpqSybxMapper;
import com.ruoyi.lwpq.domain.LwpqSybx;
import com.ruoyi.lwpq.service.ILwpqSybxService;
import com.ruoyi.common.core.text.Convert;

/**
 * sybxService业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-17
 */
@Service
public class LwpqSybxServiceImpl implements ILwpqSybxService 
{
    @Autowired
    private LwpqSybxMapper lwpqSybxMapper;

    /**
     * 查询sybx
     * 
     * @param id sybx主键
     * @return sybx
     */
    @Override
    public LwpqSybx selectLwpqSybxById(Long id)
    {
        return lwpqSybxMapper.selectLwpqSybxById(id);
    }

    /**
     * 查询sybx列表
     * 
     * @param lwpqSybx sybx
     * @return sybx
     */
    @Override
    public List<LwpqSybx> selectLwpqSybxList(LwpqSybx lwpqSybx)
    {
        return lwpqSybxMapper.selectLwpqSybxList(lwpqSybx);
    }

    /**
     * 新增sybx
     * 
     * @param lwpqSybx sybx
     * @return 结果
     */
    @Transactional
    @Override
    public int insertLwpqSybx(LwpqSybx lwpqSybx)
    {
        lwpqSybx.setCreateTime(DateUtils.getNowDate());
        int rows = lwpqSybxMapper.insertLwpqSybx(lwpqSybx);
        insertLwpqCbry(lwpqSybx);
        return rows;
    }

    /**
     * 修改sybx
     * 
     * @param lwpqSybx sybx
     * @return 结果
     */
    @Transactional
    @Override
    public int updateLwpqSybx(LwpqSybx lwpqSybx)
    {
        lwpqSybx.setUpdateTime(DateUtils.getNowDate());
        lwpqSybxMapper.deleteLwpqCbryBySybxId(lwpqSybx.getId());
        insertLwpqCbry(lwpqSybx);
        return lwpqSybxMapper.updateLwpqSybx(lwpqSybx);
    }

    /**
     * 批量删除sybx
     * 
     * @param ids 需要删除的sybx主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLwpqSybxByIds(String ids)
    {
        lwpqSybxMapper.deleteLwpqCbryBySybxIds(Convert.toStrArray(ids));
        return lwpqSybxMapper.deleteLwpqSybxByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除sybx信息
     * 
     * @param id sybx主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteLwpqSybxById(Long id)
    {
        lwpqSybxMapper.deleteLwpqCbryBySybxId(id);
        return lwpqSybxMapper.deleteLwpqSybxById(id);
    }

    /**
     * 新增${subTable.functionName}信息
     * 
     * @param lwpqSybx sybx对象
     */
    public void insertLwpqCbry(LwpqSybx lwpqSybx)
    {
        List<LwpqCbry> lwpqCbryList = lwpqSybx.getLwpqCbryList();
        Long id = lwpqSybx.getId();
        if (StringUtils.isNotNull(lwpqCbryList))
        {
            List<LwpqCbry> list = new ArrayList<LwpqCbry>();
            for (LwpqCbry lwpqCbry : lwpqCbryList)
            {
                lwpqCbry.setSybxId(id);
                list.add(lwpqCbry);
            }
            if (list.size() > 0)
            {
                lwpqSybxMapper.batchLwpqCbry(list);
            }
        }
    }
}
